package com.hotkid.ceo.jetpack.myjetpackmvvm.ui.login

import androidx.lifecycle.MutableLiveData
import com.hotkid.ceo.jetpack.myjetpackmvvm.base.BaseViewModel
import com.hotkid.ceo.jetpack.myjetpackmvvm.data.http.ApiException

/**
 * ClassName:loginViewModel
 * Package:com.hotkid.ceo.jetpack.myjetpackmvvm.ui.login
 * Description:
 * @date:2021/3/25 2:11 PM
 * @author:774169396@qq.com
 */
class LoginViewModel :BaseViewModel(){
    protected val loginRepository by lazy { LoginRepository() }
    val loginState= MutableLiveData<Boolean>()


    fun login(username:String,password:String?){
        launch(
                block = {
                    val loginData = loginRepository.login(username, password)
                    loginState.value=loginData.code()==0
                },
                error = {
                    if (it is ApiException) {
                        // -1001 代表登录失效，需要重新登录
                        if (-1001 == it.code) {
                            loginState.value = false
                        }
                    }
                },
                cancel = {

                },
                false
        )
    }
}